import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/common/nodata.dart';
import 'package:magic_car/user/examquestion.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/colors.dart';
import 'package:magic_car/util/config.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class ExamRecPage extends StatefulWidget {
  ExamRecPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<ExamRecPage> {
  List formList;
  initState() {
    super.initState();
    formList = [
      // {
      //   "title": '车牌号111111',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
    ];
    loaddata();
  }

  //加载数据
  loaddata() async {
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["id"] = pref.getString('uid');

    print(map);

    HttpHelper.post(HttpHelper.API_GetScore, (data) async {
      if (data != null) {
        print(data);

        if (data["code"].toString() == "1") {
          // List temp = [];
          // temp.add(data["info"]);
          setState(() {
            formList = data["info"];
          });
        } else {
          Toast.show(data["msg"], context);
        }
      }
    }, params: map);
  }

  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;

    for (var item in formList) {
      tiles.add(Padding(
          padding: new EdgeInsets.fromLTRB(15, 15, 15, 0),
          child: Container(
            decoration: new BoxDecoration(
              color: Colors.white,
            ),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 15, 20, 10),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 2,
                          child: new Text(
                            '考试时间：' + item["createtime"],
                            style: new TextStyle(fontSize: 12),
                          )),
                      Expanded(
                        flex: 1,
                        child: new Align(
                            alignment: Alignment.centerRight,
                            child: item["right"] * 2 >= 90
                                ? new Text('合格')
                                : new Text(
                                    '不合格',
                                    style: TextStyle(color: Colors.red),
                                  )),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                  child: Row(
                    children: <Widget>[
                      Text('总试题数：50')
                      // Text('总试题数：'+item["allnum"].toString())
                    ],
                  ),
                ),
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                  child: Row(
                    children: <Widget>[
                      Text('总    分：' + item["allsc"].toString())
                    ],
                  ),
                ),
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                  child: Row(
                    children: <Widget>[
                      //  Text('正确：'+item["right"].toString()+'    错误：'+item["wrong"].toString()
                      // )
                      Text('正确：' + item["right"].toString()),
                      Padding(
                          padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                          child: GestureDetector(
                              onTap: () {
                                Navigator.of(context)
                                    .push(new MaterialPageRoute(builder: (_) {
                                  return new ExamQuesPage(item["id"], "1");
                                }));
                              },
                              child: Text('错误：' + item["wrong"].toString(),style: TextStyle(color: Colors.red),))),
                      Padding(
                          padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
                          child: GestureDetector(
                              onTap: () {
                                Navigator.of(context)
                                    .push(new MaterialPageRoute(builder: (_) {
                                  return new ExamQuesPage(item["id"], "2");
                                }));
                              },
                              child: Text('未回答：' + (50-item["right"]-item["wrong"]).toString(),style: TextStyle(color:Colours.app_main),))),
                      // Text('正确：'+item["right"].toString()+'    错误：'+item["wrong"].toString() +
                      // '    未回答：'+(50-item["right"]-item["wrong"]).toString())
                    ],
                  ),
                ),
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 0, 20, 10),
                  child: Row(
                    children: <Widget>[
                      Text('考试得分：' + (item["right"] * 2).toString())
                    ],
                  ),
                ),
                Padding(
                  padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
                  child: item["right"] * 2 >= 90
                      ? new Text('')
                      : Align(
                          alignment: Alignment.centerRight,
                          child: new Container(
                            child: new Material(
                              //INK可以实现装饰容器
                              child: new Ink(
                                //用ink圆角矩形
                                // color: Colors.red,
                                decoration: new BoxDecoration(
                                  //背景
                                  color: Colors.white,
                                  //设置四周圆角 角度
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(18.0)),
                                  //设置四周边框
                                  border: new Border.all(
                                      width: 1,
                                      color: Colours.app_main),
                                ),
                                child: new InkWell(
                                    //圆角设置,给水波纹也设置同样的圆角
                                    //如果这里不设置就会出现矩形的水波纹效果
                                    borderRadius:
                                        new BorderRadius.circular(15.0),
                                    //设置点击事件回调
                                    onTap: () {
                                      Navigator.pushNamed(context, "applytest");
                                    },
                                    child: Container(
                                      //设置 child 居中
                                      alignment: Alignment(0, 0),
                                      height: 36,
                                      width: 105,
                                      child: Text(
                                        "申请补考",
                                        style: TextStyle(
                                            color: Colours.app_main)
                                      ),
                                    )),
                              ),
                            ),
                          ),
                          // child: new RaisedButton(
                          //   child: Text('申请补考'),
                          //   onPressed: () {},
                          // ),
                        ),
                ),
              ],
            ),
          )));
    }
    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          titleSpacing: 0,
          title: new Text('考试记录',style: TextStyle(fontSize: Config.title_size),),
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: new Center(
          child: Container(
              decoration: new BoxDecoration(
                color: Colours.base_page_color,
              ),
              child:
              formList.length==0?Nodata():
               new ListView(
                children: <Widget>[buildcontent()],
              )),
        ));
  }
}
